package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.DemandDao;
import cn.gson.boot.model.dao.SalesDao;
import cn.gson.boot.model.dao.StageDao;
import cn.gson.boot.model.mapper.CompetitorMapper;
import cn.gson.boot.model.mapper.DemandMapper;
import cn.gson.boot.model.mapper.StageMapper;
import cn.gson.boot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class DemandService {
    @Autowired
    DemandMapper demandMapper;
    @Autowired
    DemandDao demandDao;
    @Autowired
    SalesDao salesDao;
    @Autowired
    StageDao stageDao;

    //客户需求分页查询
    public List<Demand> selectAllDemand(String search,String type,String empName) {
        return demandMapper.selectAllDemand(search,type,empName);
    }

    //新增客户需求
    public void addDemand(Demand demand) {
        String Nowtime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());//一个string类型的当前时间
        java.sql.Date mysqldate = java.sql.Date.valueOf(Nowtime);
        demand.setDemandSc(0);
        Sales sales = salesDao.findById(demand.getSales().getSalesId()).get();
        Stage stage = new Stage();
        stage.setStageId(2);
        sales.setStage(stage);
        Stage stage1 = stageDao.findById(stage.getStageId()).get();
        int i = Integer.parseInt(sales.getSalesPossibility().substring(0,2));
        if(demandMapper.selectSalesDemand(demand.getSales().getSalesId()).size()==0){
            if(i>=60){
                sales.setSalseLog("   " + sales.getSalseLog()+ "日期:"+mysqldate+"\t"+"阶段:"+stage1.getStageName());
                salesDao.save(sales);
            }else{
                i = i+20;
                sales.setSalesPossibility(i+"%");
                sales.setSalseLog("   " + sales.getSalseLog() + "日期:"+mysqldate+"\t"+"阶段:"+stage1.getStageName());
                salesDao.save(sales);
            }
        }else{
            sales.setSalseLog("\t"+ sales.getSalseLog() + "日期:"+mysqldate+"\t"+"阶段:"+stage1.getStageName());
            salesDao.save(sales);

        }
        demandDao.save(demand);
    }

    //查询需要修改的客户需求
    public List<Demand> upDemand(Integer demandId) {
        return demandMapper.upDemand(demandId);
    }

    //删除客户需求
    public void delDemand(Integer demandId) {
        Demand demand = demandDao.findById(demandId).get();
        if(demandMapper.selectSalesDemand(demand.getSales().getSalesId()).size()==1){
            Sales sales = salesDao.findById(demand.getSales().getSalesId()).get();
            int i = Integer.parseInt(sales.getSalesPossibility().substring(0,2));
            if(i>=30){
                i = i-20;
                sales.setSalesPossibility(i+"%");
            }
            salesDao.save(sales);
            demand.setDemandSc(1);
            demandDao.save(demand);
        }else {
            demand.setDemandSc(1);
            demandDao.save(demand);
        }
    }

    //客户需求客户列表
    public List<Kehu> selectKh(String khsearch,String empName) {
        return demandMapper.selectKh(khsearch,empName);
    }

    //对应机会列表
    public List<Sales> selectSales(String khName) {
        return demandMapper.selectSales(khName);
    }

    //重要程度列表
    public List<Degree> selectdegree() {
        return demandMapper.selectdegree();
    }

    //销售机会页面已生成客户需求不可删除
    public List<Demand> selectSalesDemand(Integer salesId) {
        return demandMapper.selectSalesDemand(salesId);
    }
}
